/*
题目：电话号码的字母组合
给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
https://leetcode.cn/problems/letter-combinations-of-a-phone-number
 */
public class LetterCombinations {
    List<String> list = new ArrayList<> ();

    public List<String> letterCombinations(String digits) {
        if (digits.length() == 0) {
            return list;
        }
        int len = digits.length();
        String[] str = new String[len];

        for (int i = 0; i < len; i++) {
            StringBuilder sb = new StringBuilder();
            char ch = digits.charAt(i);
            switch (ch) {
                case '2' : sb.append("abc");
                    break;
                case '3' : sb.append("def");
                    break;
                case '4' : sb.append("ghi");
                    break;
                case '5' : sb.append("jkl");
                    break;
                case '6' : sb.append("mno");
                    break;
                case '7' : sb.append("pqrs");
                    break;
                case '8' : sb.append("tuv");
                    break;
                case '9' : sb.append("wxyz");
                    break;
            }
            str[i] = sb.toString();
        }

        backTrack(str, 0, new StringBuilder());

        return list;
    }

    private void backTrack(String[] str, int index, StringBuilder sb) {
        if (index >= str.length) {
            list.add(sb.toString());
            return;
        }
        int i = index;
        for (int j = 0; j < str[i].length(); j++) {
            char x = str[i].charAt(j);
            sb.append(x);
            backTrack(str, index + 1, sb);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}
